package com.yantong.quartz.job.manage;

import com.yantong.constant.BalanceLogFlowType;
import com.yantong.constant.BalanceLogStyle;
import com.yantong.constant.ProductType;
import com.yantong.pojo.BalanceLog;
import com.yantong.service.BalanceLogService;
import com.yantong.pojo.OrderSplitInfo;
import com.yantong.pojo.vo.JuheOrderSplitVo;
import com.yantong.mapper.JuheOrderMapper;
import com.yantong.mapper.OrderSplitInfoMapper;
import com.yantong.pojo.Member;
import com.yantong.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author dehua
 * @title: OrderTimerManage
 * @date 2021/8/18  15:12
 */
@Service
public class OrderTimerManage {
    @Autowired
    private JuheOrderMapper juheOrderMapper;
    @Autowired
    private OrderSplitInfoMapper orderSplitInfoMapper;
    @Autowired
    private MemberService memberService;
    @Autowired
    private BalanceLogService balanceLogService;

    /**
     * 聚合支付订单  每天凌晨一点结算
     */
    public void juheOrderSplitSettlement(){
        //聚合
        //聚合支付退款期限为7天
        List<JuheOrderSplitVo> juheOrderSplitVoList = juheOrderMapper.getJuheOrderSplitByPayTime(7);
        //大礼包
        List<JuheOrderSplitVo> giftOrderSplitVoList = juheOrderMapper.getGiftOrderSplitByPayTime(2);
        juheOrderSplitVoList.addAll(giftOrderSplitVoList);

        for ( JuheOrderSplitVo model : juheOrderSplitVoList){
            Member teamMember = memberService.getByKey(model.getTeam_id());
            if( teamMember == null ){
                continue;
            }
           BigDecimal money = model.getMoney();
            //'结算方式1-商户结算（余额），2-交易分账（易票联）'
           Integer splitType = model.getSplit_type();

            //商户结账  释放未结算余额
            if(splitType == 1){
                teamMember.setCredit2(teamMember.getCredit2().add(money));
                teamMember.setNoSettlementCredit2(teamMember.getNoSettlementCredit2().subtract(money));

                String title = "订单未结算余额转入";
                String remark = "用户使用聚合支付购买大礼包，送的券核销使用后结算余额ds";
                if( model.getOrder_type() != null ){
                    if( model.getOrder_type().intValue() == ProductType.Spellgroup.getType() ){
                        //拼团大礼包
                        remark = "用户购买拼团大礼包，送的券核销使用后结算余额ds";
                    }else if( model.getOrder_type().intValue() == ProductType.FreeCharge.getType() ){
                        //大礼包
                        remark = "用户购买大礼包，送的券核销使用后结算余额ds";
                    }
                }
                //余额变更记录
                BalanceLog supplyBalanceLog = new BalanceLog();
                supplyBalanceLog.setMid(teamMember.getId());
                supplyBalanceLog.setMobile(teamMember.getMobile());
                supplyBalanceLog.setRealname(teamMember.getRealname());
                supplyBalanceLog.setStatus(0);
                supplyBalanceLog.setType(0);
                supplyBalanceLog.setStyle(BalanceLogStyle.JUHE_SETTLEMENT.getCode());
                supplyBalanceLog.setCredittype("credit2");
                supplyBalanceLog.setBeforeamount(teamMember.getCredit2());
                supplyBalanceLog.setMoney(money);
                supplyBalanceLog.setAfteramount(teamMember.getCredit2().add(money));
                supplyBalanceLog.setCreateTime(new Date());
                supplyBalanceLog.setCentreId(teamMember.getCentreId());
                supplyBalanceLog.setTitle(title);
                supplyBalanceLog.setRemark(remark);
                supplyBalanceLog.setSource(0);
                supplyBalanceLog.setFlowtype(BalanceLogFlowType.JUHE_SETTLEMENT.getCode());
                balanceLogService.save(supplyBalanceLog);
            }else{
                teamMember.setSettlementMoney(teamMember.getSettlementMoney().add(money));
                teamMember.setNoSettlementMoney(teamMember.getNoSettlementMoney().subtract(money));
            }
            memberService.updateMemberAsync(teamMember);

            OrderSplitInfo record = new OrderSplitInfo();
            record.setId(model.getOrderSplitInfoId());
            record.setStatus(1);
            record.setStatusTime(new Date());
            orderSplitInfoMapper.updateByPrimaryKeySelective(record);
        }
    }
}
